package com.ruoyi.web.controller.patent;

import java.util.List;

import com.ruoyi.domain.TycPatentZd;
import com.ruoyi.domain.YcPersonal;
import com.ruoyi.domain.YcTaskScore;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.ITycPatentZdService;
import com.ruoyi.service.IYcPersonalService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.TycPatent;
import com.ruoyi.service.ITycPatentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 专利Controller
 *
 * @author ruoyi
 * @date 2020-09-09
 */
@Controller
@RequestMapping("/ruoyi/patent")
public class TycPatentController extends BaseController {
    private String prefix = "ruoyi/patent";

    @Autowired
    private ITycPatentService tycPatentService;
    @Autowired
    private ITycPatentZdService iTycPatentZdService;
    @Resource
    private IYcPersonalService iYcPersonalService;
    @RequiresPermissions("ruoyi:patent:view")
    @GetMapping()
    public String patent() {
        return prefix + "/patent";
    }

    /**
     * 查询专利列表
     */
    @RequiresPermissions("ruoyi:patent:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TycPatent tycPatent) {
        startPage();
        List<TycPatent> list = tycPatentService.selectTycPatentList(tycPatent);
        tycPatent.setUserId(ShiroUtils.getUserId());
        return getDataTable(list);
    }

    /**
     * 导出专利列表
     */
    @RequiresPermissions("ruoyi:patent:export")
    @Log(title = "专利", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TycPatent tycPatent) {
        List<TycPatent> list = tycPatentService.selectTycPatentList(tycPatent);
        ExcelUtil<TycPatent> util = new ExcelUtil<TycPatent>(TycPatent.class);
        return util.exportExcel(list, "patent");
    }

    /**
     * 新增专利
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存专利
     */
    @RequiresPermissions("ruoyi:patent:add")
    @Log(title = "专利", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TycPatent tycPatent) {
        //查询字典赋予 积分 奖金 数据
        tycPatent.setGrade(iTycPatentZdService.selectTycPatentZdGrade(tycPatent.getPtype()).getAward());
        tycPatent.setBonus(iTycPatentZdService.selectTycPatentZdGrade(tycPatent.getPtype()).getScore());
        //获取当前登录用户信息存入上传人
        tycPatent.setShname(ShiroUtils.getLoginName());
        tycPatent.setUserId(ShiroUtils.getUserId());
        return toAjax(tycPatentService.insertTycPatent(tycPatent));
    }

    /**
     * 修改专利
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TycPatent tycPatent = tycPatentService.selectTycPatentById(id);
        mmap.put("tycPatent", tycPatent);
        return prefix + "/edit";
    }

    /**
     * 修改保存专利
     */
    @RequiresPermissions("ruoyi:patent:edit")
    @Log(title = "专利", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TycPatent tycPatent) {
        return toAjax(tycPatentService.updateTycPatent(tycPatent));
    }

    /**
     * 删除专利
     */
    @RequiresPermissions("ruoyi:patent:remove")
    @Log(title = "专利", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tycPatentService.deleteTycPatentByIds(ids));
    }

    /**
     * 查询课题字典表
     *
     * @param tycPatentZd
     * @return
     */
    @GetMapping("/score")
    @ResponseBody
    public List<TycPatentZd> plans(TycPatentZd tycPatentZd) {
        //查询课题字典表所有的属性
        List<TycPatentZd> tycPatent = iTycPatentZdService.selectTycPatentZdList(tycPatentZd);
        return tycPatent;

    }
    /**
     * 提交申请备案
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        //更新申报状态为待备案
        tycPatentService.submitApplications(id);
        return success();
    }


    /**
     * 提交申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitForApproval")
    @ResponseBody
    public AjaxResult submitForApproval(Long id) {
        //更新审批状态为待审批
        tycPatentService.updateSubmitForApproval(id);
        return success();
    }

    /**
     * 撤销申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocation")
    @ResponseBody
    public AjaxResult revocation(Long id) {
        //更新审批状态为未备案
        tycPatentService.withdrawClaim(id);
        return success();
    }

    /**
     *显示个人信息
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("/chakan/{userId}")
    public String chakan(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        YcPersonal ycPersonal = iYcPersonalService.selectYcPersonalById(userId);
        System.out.println(ycPersonal);
        mmap.put("ycPersonal", ycPersonal);
        return prefix + "/particularsss";
    }
}
